<template>
  <div class="app-container">
    <el-page-header @back="goBack" style="margin-bottom: 20px;" content="详情页面">
    </el-page-header>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="宿舍楼" prop="buildingId">
        <el-select clearable v-model.number="queryParams.buildingId" placeholder="请选择" filterable
          @change="changeBuilding">
          <el-option v-for="(item, index) in buildingList" :key="index" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单元" prop="unitId">
        <el-select clearable v-model.number="queryParams.unitId" placeholder="请选择" filterable @change="changeUnit">
          <el-option v-for="item in unitList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼层" prop="floorId">
        <el-select clearable v-model.number="queryParams.floorId" placeholder="请选择" filterable @change="changeFloor">
          <el-option v-for="item in floorList" :key="item.id" :label="item.floor" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="宿舍" prop="dormitoryId">
        <el-select clearable v-model.number="queryParams.dormitoryId" placeholder="请选择" filterable>
          <el-option v-for="item in dormitoryList" :key="item.id" :label="item.roomNo" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="床位编号" prop="bedCode">
        <el-input v-model="queryParams.bedCode" placeholder="请输入床位编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:bed:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:bed:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:bed:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:bed:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="宿舍id" align="center" prop="DormId" />
      <el-table-column label="床位编号" align="center" prop="BedCode" />
      <el-table-column label="所属状态(1已分配0未分配)" align="center" prop="HasStudent" />
      <el-table-column label="学号" align="center" prop="XH" />
      <el-table-column label="学生姓名" align="center" prop="XSXM" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bed:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:bed:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改宿舍床位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="宿舍id" prop="dormitoryId">
          <el-input v-model.number="form.dormitoryId" placeholder="请输入宿舍id" />
        </el-form-item>
        <el-form-item label="床位编号" prop="bedCode">
          <el-input v-model.number="form.bedCode" placeholder="请输入床位编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBed, getBed, delBed, addBed, updateBed } from "@/api/dormitory/basedata/bed";
import { listBuilding } from "@/api/dormitory/basedata/building";
import { listUnit } from "@/api/dormitory/basedata/unit";
import { listFloor } from "@/api/dormitory/basedata/floor";
import { listDormitory } from "@/api/dormitory/basedata/dormitory";

export default {
  name: "Bed",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 宿舍床位表格数据
      bedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dormitoryId: Number(this.$route.query.dormitoryId),
        bedCode: null,
        buildingId: Number(this.$route.query.buildingId),
        unitId: Number(this.$route.query.unitId),
        floorId: Number(this.$route.query.floorId),
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dormitoryId: [
          { required: true, message: "宿舍id不能为空", trigger: "blur" }
        ],
        bedCode: [
          { required: true, message: "床位编号不能为空", trigger: "blur" }
        ]
      },
      buildingList: [],
      unitList: [],
      floorList: [],
      dormitoryList: []
    };
  },
  mounted() {
    this.getList();
    this.getBuildingList()
    this.getUnitList()
    this.getFloorList()
    this.getDormitoryList()
  },
  methods: {
    /** 查询宿舍床位列表 */
    getList() {
      this.loading = true;
      listBed(this.queryParams.dormitoryId).then(response => {
        this.bedList = response.data;
        this.total = response.data.length;
        this.loading = false;
      });
    },
    // 改变宿舍楼
    changeBuilding(value) {
      this.queryParams.unitId = null
      this.queryParams.floorId = null
      this.queryParams.dormitoryId = null
    },
    // 改变单元
    changeUnit(value) {
      this.queryParams.dormitoryId = null
      this.queryParams.floorId = null
    },
    // 改变单元
    changeFloor(value) {
      this.queryParams.dormitoryId = null
    },
    // 获取宿舍楼列表
    getBuildingList() {
      listBuilding({ pageSize: 1000 }).then(res => {
        this.buildingList = res.rows
      })
    },

    // 获取单元列表
    getUnitList() {
      listUnit({ pageSize: 1000, buildingId: this.queryParams.buildingId }).then(res => {
        this.unitList = res.rows
      })
    },
    // 获取楼层列表
    getFloorList() {
      listFloor({ pageSize: 1000, buildingId: this.queryParams.buildingId }).then(res => {
        this.floorList = res.rows
      })
    },
    // 获取宿舍列表
    getDormitoryList() {
      listDormitory({ pageSize: 1000, ...this.queryParams, dormitoryId: null }).then(res => {
        this.dormitoryList = res.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dormitoryId: null,
        bedCode: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (!this.queryParams.dormitoryId) {
        this.$message({
          message: '请先选择宿舍',
          type: 'error',
        })
        return
      }
      this.getList();

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加宿舍床位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBed(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改宿舍床位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBed(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBed(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let ids = [row.id]
      if (row.id instanceof Object) ids = this.ids

      this.$modal.confirm('是否确认删除宿舍床位编号为"' + ids + '"的数据项？').then(function () {
        return delBed(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bed/export', {
        ...this.queryParams
      }, `bed_${new Date().getTime()}.xlsx`)
    },
    // 返回宿舍页
    goBack() {
      this.$router.push('/dormitory/baseinfo/dormitoryInfo')
    }
  },
  watch: {
    'queryParams': {
      handler(newValue) {
        this.getBuildingList()
        this.getUnitList()
        this.getFloorList()
        this.getDormitoryList()
      },
      deep: true // 开启深度监视
    }
  }
};
</script>
